/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.view;

import com.minierp.model.CooperBill;
import com.minierp.model.CooperBillExt;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class CooperBillListExcelView extends AbstractExcelView {
	public void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		response.reset();
		response.setContentType("APPLICATION/vnd.ms-excel");
		response.setHeader("Content-Disposition",
				"attachment;filename=\"CooperBillList.xls\"");
		List list = (List) model.get("list");
		HSSFSheet sheet = workbook.createSheet("list");
		String remainTotal = "0";
		if (model.get("costTotal") != null) {
			remainTotal = (new BigDecimal(model.get("costTotal").toString()))
					.add(new BigDecimal(model.get("adjustTotal").toString()))
					.subtract(new BigDecimal(model.get("payTotal").toString()))
					.toString();
		}

		String str = "外协金额：" + model.get("costTotal") + "元(已优惠："
				+ model.get("favTotal") + "元）调账： " + model.get("adjustTotal")
				+ "元";
		str = str + "  已付：" + model.get("payTotal") + "元（其中预付款："
				+ model.get("imprestTotal") + "元） 欠款： " + remainTotal + "元";
		HSSFRow r = sheet.createRow(0);
		HSSFCell c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		c.setCellValue(new HSSFRichTextString(str));
		sheet.addMergedRegion(new Region(0, 0, 0, 9));
		str = "面积合计：" + model.get("squareTotal") + "平方米";
		str = str + "  长度合计：" + model.get("lengMTotal") + "米 "
				+ model.get("lengCmTotal") + "公分 ";
		str = str + "  合同单数：" + list.size() + "笔";
		r = sheet.createRow(1);
		c = r.createCell(0);
		r.createCell(1);
		r.createCell(2);
		r.createCell(3);
		r.createCell(4);
		r.createCell(5);
		r.createCell(6);
		r.createCell(7);
		r.createCell(8);
		r.createCell(9);
		r.createCell(10);
		r.createCell(11);
		r.createCell(12);
		r.createCell(13);
		c.setCellValue(new HSSFRichTextString(str));
		sheet.addMergedRegion(new Region(0, 0, 0, 13));
		r = sheet.createRow(2);
		r.createCell(0).setCellValue(new HSSFRichTextString("单号"));
		r.createCell(1).setCellValue(new HSSFRichTextString("外协日期"));
		r.createCell(2).setCellValue(new HSSFRichTextString("外协单位"));
		r.createCell(3).setCellValue(new HSSFRichTextString("摘要"));
		r.createCell(4).setCellValue(new HSSFRichTextString("交货日期"));
		r.createCell(5).setCellValue(new HSSFRichTextString("金额"));
		r.createCell(6).setCellValue(new HSSFRichTextString("调帐"));
		r.createCell(7).setCellValue(new HSSFRichTextString("已付"));
		r.createCell(8).setCellValue(new HSSFRichTextString("欠款"));
		r.createCell(9).setCellValue(new HSSFRichTextString("业务员"));
		r.createCell(10).setCellValue(new HSSFRichTextString("开单人"));
		r.createCell(11).setCellValue(new HSSFRichTextString("审核状态"));
		r.createCell(12).setCellValue(new HSSFRichTextString("付款状态"));
		r.createCell(13).setCellValue(new HSSFRichTextString("收货状态"));
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");

		for (int os = 0; os < list.size(); ++os) {
			Object[] objs = (Object[]) list.get(os);
			CooperBill b = (CooperBill) objs[0];
			CooperBillExt be = (CooperBillExt) objs[1];
			String estName = (String) objs[2];
			if (b.getLinkman() != null && !b.getLinkman().equals("")) {
				estName = estName + "(" + b.getLinkman() + ")";
			}

			String gatherDate = "";
			if (b.getGatherDate() != null) {
				gatherDate = sf.format(b.getGatherDate());
			}

			String exaStatus = "";
			String payStatus = "";
			String gatStatus = "";
			if (be.getExamined().equals("Y")) {
				exaStatus = "已审核";
			} else {
				exaStatus = "未审核";
			}

			if (be.getPaied().equals("Y")) {
				payStatus = "已付清";
			} else {
				payStatus = "未付清";
			}

			if (be.getGathered().equals("Y")) {
				gatStatus = "已收完";
			} else if (be.getGathered().equals("P")) {
				gatStatus = "未收完";
			} else {
				gatStatus = "未收货";
			}

			r = sheet.createRow(os + 3);
			r.createCell(0).setCellValue(
					new HSSFRichTextString(b.getBillCode()));
			r.createCell(1).setCellValue(
					new HSSFRichTextString(sf.format(b.getCooperDate())));
			r.createCell(2).setCellValue(new HSSFRichTextString(estName));
			r.createCell(3)
					.setCellValue(new HSSFRichTextString(b.getContent()));
			r.createCell(4).setCellValue(new HSSFRichTextString(gatherDate));
			r.createCell(5).setCellValue(b.getCost().doubleValue());
			r.createCell(6).setCellValue(be.getAdjustCost().doubleValue());
			r.createCell(7).setCellValue(be.getPaiedCost().doubleValue());
			r.createCell(8).setCellValue(
					b.getCost().add(be.getAdjustCost())
							.subtract(be.getPaiedCost()).doubleValue());
			r.createCell(9).setCellValue(
					new HSSFRichTextString(b.getManagerName()));
			r.createCell(10).setCellValue(
					new HSSFRichTextString(b.getCreaterName()));
			r.createCell(11).setCellValue(new HSSFRichTextString(exaStatus));
			r.createCell(12).setCellValue(new HSSFRichTextString(payStatus));
			r.createCell(13).setCellValue(new HSSFRichTextString(gatStatus));
		}

		ServletOutputStream arg20 = response.getOutputStream();
		workbook.write(arg20);
		arg20.flush();
		arg20.close();
	}
}